$(function () {
    var detaData;
    var detailData = {
        goods_id: $.getUrl("goods_id")
    }
    init()

    function init() {
        addCar()
        getDetail()
    }
    // 发送ajax请求
    function getDetail() {
        $.ajax({
            type: "get",
            url: "goods/detail",
            data: detailData,
            dataType: "json",
            success: function (res) {
                console.log(res);
                if (res.meta.status == 200) {
                    // 成功
                    detaData = res.data
                    var html = template("detailtp", detaData)
                    $(".pyg_view").html(html)
                    var gallery = mui('.mui-slider');
                    gallery.slider({
                        interval: 1000 //自动轮播周期，若为0则不自动播放，默认为0；
                    });
                } else {
                    // 失败
                }

            }
        });
    }



    // 点击加入购物车
    function addCar() {
        $('#add_gwc').on("tap", function () {
            // 判断有没有token
            var str = sessionStorage.getItem("userinfo")
            if (!str) {
                // 跳到登录页面
                // 储存当前的url
                sessionStorage.setItem("thisurl", location.href)
                // 延迟加载页面
                setTimeout(function () {
                    location.href = "login.html"
                }, 1000)

            } else {

                var carData = {
                    cat_id: detaData.cat_id,
                    goods_id: detaData.goods_id,
                    goods_name: detaData.goods_name,
                    goods_number: detaData.goods_number,
                    goods_price: detaData.goods_price,
                    goods_small_logo: detaData.goods_small_logo,
                    goods_weight: detaData.goods_weight
                }
                carDatastr = JSON.stringify(carData);
                var token = JSON.parse(str).token;
                // 发送ajax、
                $.ajax({
                    type: "post",
                    url: "my/cart/add",
                    data: {
                        info:carDatastr
                    },
                    headers:{
                        Authorization:token
                    },
                    dataType: "json",
                    success: function (res) {
                            console.log(res);
                            
                    }
                });
            }

        })
    }







})